package com.nova.scenic.projectlibs.util;

import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;

import com.nova.scenic.projectlibs.util.debug.MyLog;
import com.nova.scenic.projectlibs.util.lifecyclehelper.LifeCycleHelper;
import com.nova.scenic.projectlibs.util.lifecyclehelper.LifeCycleLogHelper;

/**
 * Created by scenic on 16/6/7.
 */
public abstract class BaseService extends Service{

    private LifeCycleHelper lifeCycleHelper = new LifeCycleLogHelper(null) {
        @Override
        public String getObjectInstance() {
            return  "@" + MyLog.getInstanceName(BaseService.this);
        }
    };

    public LifeCycleHelper getLifeCycleHelper() {
        return lifeCycleHelper;
    }

    public void setLifeCycleHelper(LifeCycleHelper lifeCycleHelper) {
        this.lifeCycleHelper = lifeCycleHelper;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onCreate();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(lifeCycleHelper != null){
            lifeCycleHelper.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onDestroy();
        }
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onStartCommand(intent, flags, startId);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onStart(intent, startId);
        }
        super.onStart(intent, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onUnbind(intent);
        }
        return super.onUnbind(intent);
    }


    @Override
    public void onTaskRemoved(Intent rootIntent) {
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onTaskRemoved(rootIntent);
        }
        super.onTaskRemoved(rootIntent);
    }

    @Override
    public void onRebind(Intent intent) {
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onRebind(intent);
        }
        super.onRebind(intent);
    }


    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onLowMemory();
        }
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        if (lifeCycleHelper != null) {
            lifeCycleHelper.onTrimMemory(level);
        }
    }





}
